package wat.mss;

import java.util.Timer;
import java.util.TimerTask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

public class Start extends Activity {
    /** Called when the activity is first created. */
	
	private static final String URL = "http://10.0.2.2:12345/Service1.svc";
	private static final String NAMESPACE = "http://mss.system.pl";
	private static final String METHOD = "OdbierzWiadomosci";
	private static final String SOAP_ACTION = "http://mss.system.pl/IService1/OdbierzWiadomosci";
	private Handler mHandler = new Handler();

	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        //Timer timer = new Timer();
        //timer.schedule(new UpdateTimeTask(), 100, 2000);

        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 100);

        //TextView tv = (TextView)findViewById(R.id.textView2);
        
        //tv.setText(makeCallToWebService(SOAP_ACTION, METHOD, "login", "Janek"));
       
        //setContentView(R.layout.main);
        //setContentView(tv);
    }
    
    private Runnable mUpdateTimeTask = new Runnable() {
    	   public void run() {

    		   TextView tv = (TextView)findViewById(R.id.textView2);
    		   String msg = makeCallToWebService(SOAP_ACTION, METHOD, "Username", "tester").replace('|', (char)10);
    		   tv.setText(tv.getText()+ (msg.contains("anyType")?"":"\n"+msg) );
    		   
    		   mHandler.postDelayed(this, 2000);
    	   }
    	};
    
    private String makeCallToWebService(String soapAction, String method,
            String attribute, String val) {
//Make request
SoapObject request = new SoapObject(NAMESPACE, method);
request.addProperty(attribute, val);
//Make SOAP envelope
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

envelope.dotNet = true;
//Send request
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try {
aht.call(soapAction, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
return result.getProperty("Messages").toString();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.toString(),
Toast.LENGTH_LONG).show();
return null;}
    }
    
}